package com.lishe.lishe.lishe.module.mall.adapter;


import android.app.Activity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.lishe.base.common.adapter.CommonAdapter;
import com.lishe.base.common.adapter.base.ItemViewDelegate;
import com.lishe.base.common.adapter.base.ViewHolder;
import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerGriadViewItemDecoration;
import com.lishe.lishe.lishe.module.mall.bean.DataItemBean;
import com.lishe.lishe.lishe.module.mall.bean.GoodsItemsBean;
import com.lishe.lishe.lishe.module.mall.bean.HomeResponse;
import com.lishe.lishe.lishe.module.mall.presenter.IndexPresenter;
import com.lishe.lishe.lishe.utils.Utils;

/**
 * 标    题：
 * 功能描述：
 * 作    者：zouxx
 * 创建时间：2018/8/14
 */

public class GoodsItemViewDelegate implements ItemViewDelegate<DataItemBean> {

    private Activity activity;

    public GoodsItemViewDelegate(Activity activity) {
        this.activity = activity;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.item_index_recyclerview;
    }

    @Override
    public boolean isForViewType(DataItemBean item, int position) {
        return item.style == 6;
    }

    @Override
    public void convert(ViewHolder holder, DataItemBean dataBean, int position) {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        holder.itemView.setLayoutParams(params);
        RecyclerView recyclerView = holder.getView(R.id.item_recyclerView);
        holder.setVisible(R.id.rl_title, true);
        ImageLoaderUtils.displayImage(activity, dataBean.iconUrl, holder.getView(R.id.icon));
        holder.setText(R.id.title, dataBean.title);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        recyclerView.setAdapter(new CommonAdapter<GoodsItemsBean>(activity, R.layout.item_header_image_recyclerview, dataBean.list) {

            @Override
            protected void convert(ViewHolder holder, GoodsItemsBean listBean, int position) {
                RecyclerView.LayoutParams params = new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                holder.itemView.setLayoutParams(params);
                ImageLoaderUtils.displayImage(activity, listBean.imgUrl, holder.getView(R.id.image));
                holder.setOnClickListener(R.id.image, v -> IndexPresenter.setOnClick(activity, listBean));
                RecyclerView recyclerView = holder.getView(R.id.recyclerView);
                recyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false));
                recyclerView.setAdapter(new CommonAdapter<GoodsItemsBean>(activity, R.layout.item_mall_scroll0, listBean.items) {

                    @Override
                    protected void convert(ViewHolder holder, GoodsItemsBean itemsBean, int position) {
                        if (position == 0) {
                            holder.setVisible(R.id.right_area, false);
                            holder.setVisible(R.id.left_area, true);
                        } else if (position == listBean.items.size() - 1) {
                            holder.setVisible(R.id.right_area, true);
                            holder.setVisible(R.id.left_area, false);
                        } else {
                            holder.setVisible(R.id.right_area, false);
                            holder.setVisible(R.id.left_area, false);
                        }
                        if (position == listBean.items.size() -1){
                            holder.setVisible(R.id.simple_layout,false);
                            holder.setVisible(R.id.more_layout, true);
                        } else {
                            holder.setVisible(R.id.more_layout, false);
                            holder.setVisible(R.id.simple_layout,true);
                        }
                        ImageLoaderUtils.displayImage(activity, itemsBean.imgUrl, holder.getView(R.id.goods_img));
                        holder.setText(R.id.goods_name, itemsBean.title);
                        holder.setText(R.id.integral, "¥" + itemsBean.price);
                        holder.itemView.setOnClickListener(v -> IndexPresenter.setOnClick(activity, itemsBean));
                    }
                });

            }
        });
    }
}
